// pages/managepage/managepage.js
const app=getApp()
import Dialog from '../../miniprogram_npm/vant-weapp/dialog/dialog';
Page({

  
  data: {
    applyIcon: "/image/shenqing.svg",
    noticeIcon: "/image/tongzhi.svg",
    notices: [],
    apply: [],
    showModal: false,
  },

  onLoad: function (options) {
    getNotices(this);
    getRecords(this);
  },
  /**
   *  添加公告
   */
  addNotice: function(e){
    wx.navigateTo({
      url: "/pages/addNotice/addNotice",
    });
   this.onLoad()
  },

/**
 * 用户管理
 */
  userManage: function(){
    wx.navigateTo({
      url: "/pages/userManage/userManage",
    });
  },

/**
 * 删除指定公告
 */
  deleteNotice: function(e){
    let _this = this;
    wx.request({
      url: 'https://dorm.dormassistant.wang:8080/notice/deleteNoticeById',
      method: "POST",
      data: {
        noticeId: e.target.id
      },
      header: {
        'Content-Type': 'application/x-www-form-urlencoded;charset=utf8'
      },
      success: function (res) {
          if(res.data.success){
            wx.showToast({
              title: "删除成功",
              icon: '',
              duration: 2000
            });
            _this.onLoad();
        }else{
            wx.showToast({
              title: "删除失败",
              icon: '',
              duration: 2000
            });
        }
      },
      fail: function (res) {
        wx.showToast({
          title: "网络故障",
          icon: '',
          duration: 2000
        });
      }
    });

  },

  /**
     * 弹窗
     */
  confirmApply: function (e) {
    var apply;
    var confirmString;
    if (e.target.dataset.apply==1){
      apply="已通过"
      confirmString="确认"
    }else{
      apply="未通过"
      confirmString = "同意"
    }
    this.setData({
      showModal: true,
      ['applyTime.date']:e.target.dataset.time,
      ['applyTime.begin']: e.target.dataset.begin,
      ['applyTime.end']: e.target.dataset.end,
      ['applyTime.isApply']: apply,
      applyId: e.target.dataset.id,
      confirmString:confirmString
    })
  },

  /**
   * 弹出框蒙层截断touchmove事件
   */
  preventTouchMove: function () {
  },

  /**
   * 隐藏模态对话框
   */
  hideModal: function () {
    this.setData({
      showModal: false
    });
  },

  /**
   * 对话框取消按钮点击事件
   */
  onCancel: function () {
    this.hideModal();
  },

  /**
   * 对话框确认按钮点击事件
   */
  passApply: function () {
    var that=this
    wx.request({
      url: 'http://localhost:8080/record/PassRecordApply',
      data:{
        recordId:that.data.applyId
      },
      method:'GET',
      success (res){
        console.log(res.data)
        that.onLoad()
      }
    })
    this.hideModal();
  }

})


//获取所有申请信息
const getRecords = function(_this){
  wx.request({
    url: 'http://localhost:8080/record/queryAll',
    success:function(res){
      var tempRecords = res.data.records;
      var tempRomms = res.data.roomList;
      var recordList = [];
      console.log(res.data)
      for (var i = 0; i < tempRecords.length; i++) {
        var c = app.extend(tempRecords[i], tempRomms[i]);
        var time = tempRecords[i].time
        var begin = tempRecords[i].begin
        var end = tempRecords[i].end
        c.time = time.substring(0, 10)
        c.begin = begin
        c.end = end
        recordList.push(c);
      }
      _this.setData({
        recordList: recordList,
      });
      console.log(res.data)
    },
    fail: function (res) {
      wx.showToast({
        title: "网络故障",
        icon: '',
        duration: 2000
      });
    }
  })
}

/**
 * 获取所有公告
 */
const getNotices = function(_this){

  wx.request({
    url: 'https://dorm.dormassistant.wang:8080/notice/queryAllNotice',
    method: "GET",
    data: {

    },
    success: function (res) {
      _this.setData({
        notices: res.data.notices
      });
      console.log(res.data);
    },
    fail: function (res) {
      wx.showToast({
        title: "网络故障",
        icon: '',
        duration: 2000
      });
    }
  });

}

const getApply = function(){
  wx.request({
    url: 'https://dorm.dormassistant.wang:8080/notice/queryAllNotice',
    method: "GET",
    data: {

    },
    success: function (res) {
      _this.setData({
        notices: res.data.notices
      });
      console.log(res.data);
    },
    fail: function (res) {
      wx.showToast({
        title: "网络故障",
        icon: '',
        duration: 2000
      });
    },

    getUserInfo(event) {
      console.log(event.detail);
    },

    onClose() {
      this.setData({ close: false });
    },


  
  });
}